1 <?
2 // This file
is an help and shouldn't be executed.
3 // You can however activate syntaxical coloration
for a better read.
4 die();

5
6 /************************** Before Everything Else: *************************/
7
8 // Include the
class:
9 include(
"db.class.php");
10
11 // Open the
base (construct the object):
12 $db =
new DB($base, $server, $user, $pass);
13
14 /************************** Basic Queries: **********************************/
15
16 // Do a query (
return the same as mysql_query):
17 $result = $db->query(
"SELECT * FROM People");
18 $result = $db->query(
"SELECT * FROM People", true); // With DEBUG
19
20 // Browse results of a query (
return a line as an object):
21 while
($line = $db->fetchNextObject($result)) {}
22 while
($line = $db->fetchNextObject()) {} // From the LAST query
23
24 // Commonly, you can copy/paste
this code for one query at a time:
25 $db->query(
"SELECT * FROM People");
26 while
($line = $db->fetchNextObject()) {
27   use($line->aField);
28 }

29
30 // But
for sub-queries you should store the result of the first queries:
31 $result = $db->query(
"SELECT * FROM Towns");
32 while
($line = $db->fetchNextObject($result)) {
33   $db->query(
"SELECT * FROM People WHERE town='{$line->name}'");
34   
while ($line2 = $db->fetchNextObject()) {
35     use($line->aField, $line2->anotherField);
36   }
37 }

38
39 // If you have a query that doesn
't return any result such as INSERT, UPDATE, DELETE... use this:
40 $db->execute(
"UPDATE People SET name=name");
41
42 /************************** Convenient Query Functions: *********************/
43
44 // Do a query and
return the unique resulting object from it (or NULL if there is no result):
45 // Equivalent to a call to query() and then to fetchNextObject().

46 $line = $db->queryUniqueObject(
"SELECT * FROM People WHERE id=2");
47 $line = $db->queryUniqueObject(
"SELECT * FROM People WHERE id=2", true); // With DEBUG
48
49 // Do a query and
return the unique resulting cell from it (or NULL if there is no result):
50 // Equivalent to a call to query() and then to fetchNextObject() and then a fetching of the first cell.

51 $age = $db->queryUniqueValue(
"SELECT age FROM People WHERE id=2");
52
53 // Return the maximum of a column
in a table, with a condition WHERE:
54 // Example:
"SELECT MAX(age) FROM People WHERE town='Paris'" is simply:
55 $max = $db->maxOf(
"age", "People", "town='Paris'");
56
57 // If there
is no condition, it's simply:
58 $max = $db->maxOfAll(
"age", "People");
59
60 // Return the number of lines
in a table, with a condition WHERE:
61 // Example:
"SELECT COUNT(*) FROM People WHERE town='Paris'" is just:
62 $count = $db->countOf(
"People", "town='Paris'");
63
64 // If there
is no condition, it's just:
65 $count = $db->countOfAll(
"People");
66
67 // All query...() methods can take a second boolean parameter to debug it or not.
68 // All those convenient methods can be imbriqued: the query
is not saved.
69
70 /************************** Other Functions: ********************************/
71
72 // Get the id of the last inserted line:

73 $lastId = $db->lastInsertedId();

74
75 // The execution time of the script (
from "new DB();"):
76 $time = $db->getExecTime();

77
78 // The number of executed queries (
from "new DB();"):
79 $queriesCount = $db->getQueriesCount();

80
81 /************************** Useless Functions: ******************************/
82
83 // Number of lines of a result (
return an integer):
84 $numLines = $db->numRows($result);
85 $numLines = $db->numRows();
// From the LAST query
86
87 // Go back to the first line
in a fetch:
88 $db->resetFetch($result);

89
90 // Close the connexion to the
base:
91 $db->close();
92
93 ?>


Gõ tìm kiếm nhanh...